home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 4 / Amiga Tools 4.iso / tools / dfue / term 4.6(?) / extras / source / term-source.lha / FixPath.c < prev    next >
C/C++ Source or Header  |  1996-03-18  |  2KB  |  123 lines

  1. /*
  2. **    FixPath.c
  3. **
  4. **    Fix the current Process search patch list by faking a CLI
  5. **
  6. **    Copyright © 1990-1996 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. **
  9. **    :ts=4
  10. */
  11.  
  12. #ifndef _GLOBAL_H
  13. #include "Global.h"
  14. #endif
  15.  
  16.     // This is how a linked list of directory search paths looks like.
  17.  
  18. struct Path
  19. {
  20.     BPTR path_Next;    // Pointer to next entry
  21.     BPTR path_Lock;    // The drawer in question; may be NULL
  22. };
  23.  
  24.     /* ClonePath(BPTR StartPath):
  25.      *
  26.      *    Make a copy of the command search path attached to a
  27.      *    CLI process.
  28.      */
  29.  
  30. STATIC BPTR
  31. ClonePath(BPTR StartPath)
  32. {
  33.     struct Path    *First,*Last,*List,*New;
  34.  
  35.     for(List = BADDR(StartPath), First = Last = NULL ; List ; List = BADDR(List->path_Next))
  36.     {
  37.         if(List->path_Lock)
  38.         {
  39.             if(New = AllocVec(sizeof(struct Path),MEMF_ANY))
  40.             {
  41.                 if(New->path_Lock = DupLock(List->path_Lock))
  42.                 {
  43.                     New->path_Next = NULL;
  44.  
  45.                     if(Last)
  46.                         Last->path_Next = MKBADDR(New);
  47.  
  48.                     if(!First)
  49.                         First = New;
  50.  
  51.                     Last = New;
  52.                 }
  53.                 else
  54.                 {
  55.                     FreeVec(New);
  56.                     break;
  57.                 }
  58.             }
  59.             else
  60.                 break;
  61.         }
  62.     }
  63.  
  64.     return(MKBADDR(First));
  65. }
  66.  
  67.     /* AttachCLI(struct WBStartup *Startup):
  68.      *
  69.      *    Attach a valid CLI structure to the current process. Requires a
  70.      *    Workbench startup message whose command search path it will
  71.      *    duplicate.
  72.      */
  73.  
  74. VOID
  75. AttachCLI(struct WBStartup *Startup)
  76. {
  77.     struct CommandLineInterface    *DestCLI;
  78.  
  79.         // Note: FreeDosObject can't free it, but the DOS
  80.         //       process termination code can.
  81.  
  82.     if(DestCLI = AllocDosObjectTagList(DOS_CLI,NULL))
  83.     {
  84.         struct Process *Dest;
  85.         struct MsgPort *ReplyPort;
  86.  
  87.         Dest = (struct Process *)FindTask(NULL);
  88.  
  89.         DestCLI->cli_DefaultStack = 4096 / sizeof(ULONG);
  90.  
  91.         Dest->pr_CLI     = MKBADDR(DestCLI);
  92.         Dest->pr_Flags     |= PRF_FREECLI;            // Mark for cleanup
  93.  
  94.         Forbid();
  95.  
  96.         ReplyPort = Startup->sm_Message.mn_ReplyPort;
  97.  
  98.             // Does the reply port data point somewhere sensible?
  99.  
  100.         if(ReplyPort && (ReplyPort->mp_Flags & PF_ACTION) == PA_SIGNAL && TypeOfMem(ReplyPort->mp_SigTask))
  101.         {
  102.             struct CommandLineInterface    *SourceCLI;
  103.  
  104.                 // Is there a CLI attached?
  105.  
  106.             if(SourceCLI = BADDR(((struct Process *)ReplyPort->mp_SigTask)->pr_CLI))
  107.             {
  108.                     // Clone the other CLI data.
  109.  
  110.                 DestCLI->cli_DefaultStack = SourceCLI->cli_DefaultStack;
  111.  
  112.                 if(SourceCLI->cli_Prompt)
  113.                     SetPrompt((STRPTR)BADDR(SourceCLI->cli_Prompt));
  114.  
  115.                 if(SourceCLI->cli_CommandDir)
  116.                     DestCLI->cli_CommandDir = ClonePath(SourceCLI->cli_CommandDir);
  117.             }
  118.         }
  119.  
  120.         Permit();
  121.     }
  122. }
  123.